package com.andreani.asian_beauties;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.aetrion.flickr.FlickrException;
import com.aetrion.flickr.REST;
import com.aetrion.flickr.groups.GroupList;
import com.aetrion.flickr.groups.GroupsInterface;
import com.aetrion.flickr.photos.Photo;
import com.aetrion.flickr.photos.PhotoList;
import com.aetrion.flickr.tags.Tag;

public class Main extends Activity { 
	public static String apiKey = "048f11027f10239f77189e62cbd9da1c";
	public static String secret= "5846c35a62894f44";
	public TextView mTv;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("main", "Main inflating");
        setContentView(R.layout.main);
        //Flickr f = new Flickr(apiKey);
        String[] flickrKeys = new String[2];
        flickrKeys[0] = apiKey; 
        flickrKeys[1] = secret;
        Log.i("main", "Getting tags");        
        new TagPopulator().execute(flickrKeys);
    }
    
    class TagPopulator extends AsyncTask<String,ArrayList<Tag>,ArrayList<Tag>> {
    	
    	// This is the group ID of the Asian Beauties App group (http://www.flickr.com/groups/aisan_beauties_apps/)
    	private String appGroupID = "1664780@N22";
    	    	
    	@Override
    	protected ArrayList<Tag> doInBackground(String... keys) {
    		
    		PhotoList mPhotoList = null;
    		// TODO Auto-generated method stub
    		REST mTransport = null;
    		Log.i("Async", "Starting transport");
    		try {
    			mTransport = new REST();
    		} catch (ParserConfigurationException e1) {
    			// TODO Auto-generated catch block
    			e1.printStackTrace();
    		}
    		GroupsInterface gI = new GroupsInterface(keys[0],keys[1],mTransport);
    		try {
    			Log.i("Async", "Searching Interfaces");
				GroupList mGroupList = (GroupList) gI.search("asian beauties app", 20 ,1);
				Log.i("Async", "Results: "+ mGroupList.size());
			} catch (FlickrException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.w("Async", "IOExc at pools interface");
    			return null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.w("Async", "SAXExc at pools interface");
    			return null;
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.w("Async", "FlickrExc at pools interface");
    			return null;
			}
    		/*
    		Log.i("Async", "Starting pool Interface");
    		
    		PoolsInterface pI = new PoolsInterface(keys[0],keys[1],mTransport);
    		try {
    			Log.i("Async", "Getting Photos");
    			mPhotoList = pI.getPhotos(appGroupID, null, 20, 1);
    			Log.i("Async", "Got Photos");
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			Log.w("Async", "IOExc at pools interface");
    			return null;
    		} catch (SAXException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			Log.w("Async", "SAXExc at pools interface");
    			return null;
    		} catch (FlickrException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			Log.w("Async", "FlickrExc at pools interface");
    			return null;
    		}*/
    		Log.i("Async", "Getting Tags from Photos");
    		Photo currPhoto;
    		//Log.v("Async", "Number of photos (should be 5):" + mPhotoList.size() );
    		for(int i = 0; i < mPhotoList.size();i++)
    		{
    			Log.i("Async", "Iteration # : ");
    			currPhoto = (Photo)mPhotoList.get(i);
    			Log.i("Async", "Publishing Progress");
    			publishProgress((ArrayList<Tag>) currPhoto.getTags());
    		}
    		Log.i("Async", "Got Tags from Photos");
    		return null;
    	}
    	
    	protected void onProgressUpdate(ArrayList<Tag> tags)
    	{
    		Log.i("Progress Update", "Getting Photos");
    		mTv = (TextView) findViewById(R.id.TagList);
    		ArrayList<String> tagValues = new ArrayList<String>();
    		Log.i("Progress Update", "Converting Tags");
    		for (int i = 0; i < tags.size();i++) tagValues.add(tags.get(i).getValue());
    		String viewText = (String) mTv.getText();
    		for (int j = 0; j < tagValues.size();j++) viewText = viewText + tagValues.get(j);
    		Log.i("Progress Update", "Setting Text of TagList");
    		mTv.setText(viewText);
    	   //for(int i = 0; i < tags.size();i++) masterTagList.addTag(tags.get(i));
    	}
    
    }
    
    
}